package com.yc.exam;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.yc.exam.web.LoginInterceptor;
import com.yc.exam.web.LoginInterceptorUser;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.sql.DataSource;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/9
 * Email haijunzhou@hnit.edu.cn
 */
@Configuration
@ComponentScan("com.yc.exam")
@EnableWebMvc
@MapperScan("com.yc.exam.mapper")
@EnableTransactionManagement
public class MvcConfig implements WebMvcConfigurer {
    //@Override
    //public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    //    configurer.enable();
    //}


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:E:\\上课视频\\138\\yc138\\exam\\src\\main\\webapp/");
        registry.addResourceHandler("/head/**")
                .addResourceLocations("file:e:\\QQ/");
    }

    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    LoginInterceptorUser loginInterceptorUser;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/paper.html","/question.html");
        registry.addInterceptor(loginInterceptorUser)
                .addPathPatterns("/exam.html");
    }



    @Bean
    DataSource dataSource(){
        DriverManagerDataSource dmds = new DriverManagerDataSource();
        dmds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dmds.setUrl("jdbc:mysql:///exam?useSSL=false&allowPublicKeyRetrieval=true");
        dmds.setUsername("root");
        dmds.setPassword("123456");
        return dmds;
    }

    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean
    DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        /**
         * 加载 mybatis 配置 ( 可选 )
         * setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        // ClassPathResource classPathResource = new ClassPathResource("mybatis.xml");
        // sqlSessionFactoryBean.setConfigLocation(classPathResource);
        /**
         * MyBatis配置类 等效于 mybatis.xml
         */
        MybatisConfiguration configuration = new MybatisConfiguration();
        /**
         * 配置日志框架 log4j, 等效于
         * <settings>
         *     <setting name="logImpl" value="LOG4J"/>
         * </settings>
         */
        configuration.setLogImpl(Log4jImpl.class);
        /**
         * MyBatis-Plus 分页插件
         */
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDialect(new MySqlDialect());
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        configuration.addInterceptor(mybatisPlusInterceptor);
        /**
         *  setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }

    @Bean
    MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }

}
